package it.lupo.model.service;

import it.lupo.model.User;
import it.lupo.repository.HibernateSessionFactory;
import it.lupo.repository.dao.UserDAO;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class UserService {

	private static UserService instance;
	
	private UserService(){}
	
	public static UserService getInstance()
	{
		if(instance==null)
			instance=new UserService();
		return instance;
	}
	
	public User check(String username,String password)
	{
		Session session = HibernateSessionFactory.getSession();
		Transaction trx = session.beginTransaction();
		try{
			UserDAO udao = new UserDAO();
			User user = udao.findById(username);
			boolean check = user!=null&&user.getPassword().equals(password);
			trx.commit();
			session.close();
			return check?user:null;
		}catch(Exception e)
		{
			e.printStackTrace();
			trx.rollback();		
			session.close();
			return null;
		}
	}
}
